package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/checkitem")
public class CheckItemController {

    @Autowired
    private CheckItemService checkItemService;

    /**
     * 新增检查项
     * @param checkItem
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        log.info("新增检查项:{}",checkItem);
        checkItemService.add(checkItem);
        return new Result(true,"新增检查项成功");
    }

    /**
     * 删除检查项
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result delete(@RequestParam Integer id){
        log.info("删除检查项:{}",id);
        checkItemService.delete(id);
        return new Result(true,"删除检查项成功");
    }

    /**
     * 根据id查询检查项
     */
    @GetMapping("/findById")
    public Result findById(Integer id){
        log.info("根据id查询检查项:{}",id);
        CheckItem checkItem = checkItemService.findById(id);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
    }

    /**
     * 编辑检查项
     * @param checkItem
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){
        log.info("修改检查项:{}",checkItem);
        checkItemService.edit(checkItem);
        return new Result(true,"修改检查项成功");
    }

    /**
     * 根据code和name分页查询检查项
     */
    @PostMapping("/findPage")
    public PageResult page(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询检查项:{}",queryPageBean);
        PageResult pageResult = checkItemService.page(queryPageBean);
        return pageResult;
    }

    @GetMapping("/findAll")
    public Result findAll(){
        List<CheckItem> checkItems = checkItemService.findAll();
        return new Result( true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItems);
    }

}
